try:
    import gdal
except:
    from osgeo import gdal
try:
    import ogr
except:
    from osgeo import gdal
import os

def get_feature_raster(shp_path, tif_path, field, folder):
    datesource = ogr.Open(shp_path)
    layer = datesource.GetLayer()
    feature = layer.GetNextFeature()
    while feature:
        value = feature.GetField(field)
        if value:
            gdal.Warp(
                os.path.join(folder, f"{int(value)}.tif"),
                tif_path,
                cutlineDSName=shp_path,
                format="GTiff",
                cutlineLayer =layer.GetName(),
                cutlineWhere=f"{field}={value}",
                cropToCutline=True,
                copyMetadata=True,
            )
        feature = layer.GetNextFeature()

    return


if __name__ == "__main__":
    shp_path = r"Z:\冉志祎\temp\G50F034009.shp"
    tif_path = r"Z:\orange\source_tif\G50F034009.jp2"
    field = "AOTU"
    folder = r"Z:\冉志祎\image"
    get_feature_raster(shp_path, tif_path, field, folder)